package 剑指offer;

import java.util.ArrayList;

/**
 * 约瑟夫环
 */
public class Offer62_JZ46 {
    public int lastRemaining(int n, int m) {
        ArrayList<Integer> list = new ArrayList<>();
        for (int i=0; i<n; i++) {
            list.add(i);
        }
        int idx = 0;
        while (n > 1) {
            idx = (idx + m - 1) % n;
            list.remove(idx);
            n--;
        }
        return list.get(0);

    }

    public int lastRemaining2(int n, int m) {
        int ans = 0;
        for (int i=2; i<=n; i++) {
            ans = (ans + m) % i;
        }
        return ans;
    }
    public static void main(String[] args) {
        boolean[] delete = new boolean[1];
        System.out.println(delete[0]);
    }
}
